import request from '@/utils/request'
// import { getToken } from '@/utils/token'
import { getStorage } from '@/utils/storage'
// 登录
export const loginAPI = (user) =>
  request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: user
  })
// 获取新的token
export const getNewToken = () =>
  request({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: 'Bearer ' + getStorage('refresh_token')
    }
  })

// 对用户关注
export const userFollowedAPI = ({ target }) =>
  request({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })

// 对用户取关
export const userUnFollowedAPI = ({ target }) =>
  request({
    url: `/v1_0/user/followings/${target}`,
    method: 'DELETE'
  })

// 取消点赞
export const unlikeAPI = ({ target }) =>
  request({
    url: `/v1_0/article/likings/${target}`,
    method: 'DELETE'
  })

// 点赞
export const likeAPI = ({ target }) =>
  request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })
// 获取所有频道
export const getAllChannelsAPI = () =>
  request({
    url: '/v1_0/channels'
  })

// 获取用户频道
export const getUserChannelsAPI = () =>
  request({
    url: '/v1_0/user/channels'
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // }
  })

// 获取用户个人信息
export const getUserInfoAPI = () =>
  request({
    url: '/v1_0/user'
  })

// 获取用户个人资料
export const userProfileAPI = () =>
  request({
    url: '/v1_0/user/profile'
  })

// 更新头像
export const updateUserPhotoAPI = (fd) =>
  request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: fd
  })

// 更新用户资料
export const updateUserProfileAPI = (obj) => {
  const theData = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const key in theData) {
    if (obj[key] === undefined) {
      delete theData[key]
    } else {
      theData[key] = obj[key]
    }
  }

  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: theData
  })
}

//  文章 - 获取列表
export const articlesListAPI = ({ channel_id, timestamp }) =>
  request({
    url: '/v1_0/articles',
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // },
    params: {
      channel_id,
      timestamp
    }
  })
// 不喜欢文章
export const dislikeNew = (target) =>
  request({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target
    }
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // }
  })
// 举报文章
export const reportArticleAPI = (target, type) =>
  request({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target,
      type
    }
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // }
  })

// 删除用户频道
export const delChannelAPI = (id) =>
  request({
    url: `/v1_0/user/channels/${id}`,
    method: 'DELETE',
    params: {
      target: id
    }
  })

// 更新覆盖频道
export const updateChannelsAPI = (channels) =>
  request({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })

// 搜索 - 联想菜单
export const suggestListAPI = ({ q }) =>
  request({
    url: '/v1_0/suggestion',
    method: 'GET',
    params: {
      q
    }
  })

// 获取查询结果
export const getSearchResultAPI = ({ page = 1, per_page = 10, q }) =>
  request({
    url: '/v1_0/search',
    params: {
      page,
      per_page,
      q
    }
  })

// 获取文章详情
export const getArticleDetail = (article_id) =>
  request({
    url: `/v1_0/articles/${article_id}`
  })

// 获取评论
export const commentsListAPI = ({ source, offset = null, limit = null }) =>
  request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source,
      offset,
      limit
    }
  })

// 对评论点赞
export const commentsLike = ({ target }) =>
  request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target
    }
  })

// 对评论取消点赞
export const commentsunLike = ({ target }) =>
  request({
    url: `/v1_0/comment/likings/${target}`,
    method: 'DELETE'
  })

// 对文章就行评论
export const commentSendAPI = ({ target, content, art_id = null }) => {
  const obj = {
    target,
    content
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }

  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}
